﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CodeBuilder2.BLL.Scanner;
using CodeBuilder2.BLL.Connection;
using CodeBuilder2.Model;
using System.ComponentModel;
using CodeBuilder2.Model.Enumeration;

namespace CodeBuilder2
{
    /// <summary>
    /// Interaction logic for LoginWnd.xaml
    /// </summary>
    public partial class LoginWnd : Window
    {
        /// <summary>
        /// 为登录/连接数据库服务器、浏览局域网中其它数据库服务器等费时操作
        /// 提供很方便实现后台执行的可能
        /// </summary>
        BackgroundWorker worker;

        public LoginWnd()
        {
            InitializeComponent();
            worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
        }

        UserOperationCommand uoCmd;
        /// <summary>
        /// 根据成员变量 uoCmd 的值，决定在后台执行何种操作
        /// </summary>
        /// <param name="sender">不知道是什么……</param>
        /// <param name="e">DoWorkEventArgs 对象</param>
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            switch (uoCmd)
            {
                case UserOperationCommand.Browse:
                    break;
                case UserOperationCommand.Connect:
                    TargetServer server = e.Argument as TargetServer;
                    try
                    {
                        // 在 Worker.DoWork 事件的处理方法中，不能访问主线程中的 UI 元素的属性
                        //string s = btnSearch.Content.ToString();
                        // 不过，这样是可以的
                        //string s = btnSearch.ToString(); 
                        bool flag = ConnectionHelper.Connect(server);
                        // 保存本次后台执行的结果到 DoWorkEventArgs 对象的 Result 属性，
                        // 以便在后续的 RunWorkerCompleted 方法中，用 RunWorkerCompletedEventArgs 对象的
                        // Result 属性来获取该值，决定如何进行进一步的操作
                        e.Result = flag;
                    }
                    catch (Exception ex)
                    {
                        e.Result = false;
                        MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 根据成员变量 uoCmd 的值，决定执行何种操作，同时根据 RunWorkerCompleted 对象
        /// 的 Result 属性，决定操作的执行细节。本方法在后台 DoWork 方法执行完毕后执行
        /// </summary>
        /// <param name="sender">不知道是什么……</param>
        /// <param name="e">RunWorkerCompletedEventArgs 对象</param>
        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            switch (uoCmd)
            {
                case UserOperationCommand.Browse:
                    break;
                case UserOperationCommand.Connect:
                    bool flag = (bool)e.Result;
                    if (flag)
                    {
                        //MessageBox.Show("连接成功");
                        (this.Owner as MainWnd).IsLogin = true;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("连接失败");
                    }
                    break;
                default:
                    break;
            }
        }
        
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            this.GetAliveHosts();
        }

        void GetAliveHosts()
        {
            PortScanner ps = new PortScanner();
            try
            {
                bool flag = ps.Scan("FRANKLAW-WIN7PC", 1433);
                if (flag)
                {
                    MessageBox.Show("连接成功");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TargetServer ts = new TargetServer();
            ts.Name = cbxServerName.Text;
            ts.UID = cbxLoginName.Text;
            ts.PWD = pbxPassword.Password;
            ts.DataBase = "master";

            // 设置成员变量的值，以供 BackgroundWorker 的 DoWork 和 RunWorkerCompleted 两个事件
            // 的处理方法来决定执行何种类型的操作
            uoCmd = UserOperationCommand.Connect;            
            worker.RunWorkerAsync(ts);
        }


    }
}
